package com.weiyi.weiman.service.comics.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weiyi.weiman.domain.Const.ComicsConst;
import com.weiyi.weiman.domain.Const.ComicsRankConst;
import com.weiyi.weiman.domain.dto.CategoryDTO;
import com.weiyi.weiman.mapper.comics.CategoryMapper;
import com.weiyi.weiman.mapper.comics.ComicsRankMapper;
import com.weiyi.weiman.service.comics.ComicsListService;
import com.weiyi.weiman.service.comics.ComicsService;
import com.weiyi.weiman.utils.RedisUtils;
import comics.BaseComics;
import comics.Category;
import comics.Comics;
import comics.ComicsRank;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhang
 * @Date 2021/5/13 下午2:24
 * @Description
 * @Version 1.0
 **/
@Service
@Slf4j
public class ComicsListServiceImpl implements ComicsListService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ComicsRankMapper rankMapper;
    @Autowired
    private ComicsService comicsService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 获取漫画分类标签的列表
     * @return
     */
    @Override
    @Cacheable(cacheNames = "comicsListService:category:list")
    public List<Category> getCategoryList() {
        String categoryKey = ComicsConst.CATEGORY_LIST_PRE;
        List<Category> list = new ArrayList<>();
        if (!redisUtils.hasKey(categoryKey)) {
            list = categoryMapper.selectList(new QueryWrapper<>());
            for (Category category : list) {
                redisUtils.lRightPush(categoryKey, JSON.toJSONString(category));
            }
        } else {
            List<String> categoryJson = redisUtils.lRange(categoryKey, 0, -1);
            for (String s : categoryJson) {
                list.add(JSON.parseObject(s, Category.class));
            }
        }
        return list;
    }

    @Override
    @Cacheable(cacheNames = "comicsListService:rank:list")
    public List<ComicsRank> getRankList() {
        List<ComicsRank> list = new ArrayList<>();
        if (!redisUtils.hasKey(ComicsConst.RANK_LIST_PRE)) {
            list = rankMapper.selectList(null);
            for (ComicsRank comicsRank : list) {
                redisUtils.lRightPush(ComicsConst.RANK_LIST_PRE, JSON.toJSONString(comicsRank));
            }
        } else {
            List<String> rankJsonList = redisUtils.lRange(ComicsConst.RANK_LIST_PRE, 0, -1);
            for (String rankJson : rankJsonList) {
                list.add(JSON.parseObject(rankJson, ComicsRank.class));
            }
        }
        redisUtils.setExpire(ComicsConst.RANK_LIST_PRE, 24, TimeUnit.HOURS);
        return list;
    }

    @Override
    @Cacheable(cacheNames = "comicsListService:topSearch:list")
    public List<BaseComics> getHotSearch(int pageNum, int pageSize) {
        if (redisUtils.hasKey(ComicsRankConst.COMICS_HOT_SEARCH)) {
            int start = (pageNum - 1) * pageSize;
            int end = start + pageSize - 1;
            Set<String> comicsIdList = redisUtils.zReverseRange(ComicsRankConst.COMICS_HOT_SEARCH, start, end);
            List<BaseComics> list = new ArrayList<>();
            for (String comicsId : comicsIdList) {
                BaseComics baseComics = new BaseComics();
                baseComics.setComicsId(Long.valueOf(comicsId));
                Comics comics = comicsService.getComics(Long.parseLong(comicsId));
                baseComics.setComicsName(comics.getComicsName());
                list.add(baseComics);
            }
            return list;
        }
        return null;
    }
}
